<?php

namespace Acme\CommonBundle\Controller;

use Symfony\Component\Security\Core\SecurityContext;
use Acme\UserBundle\Controller\SecurityController;
use Acme\EventBundle\Entity\Inscription;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * @Route("/email")
 */
class CronController extends Controller {
	
	/**
	 * @Route("/cron", name="event_cron")
	 * @Template()
	 */
	public function cronAction() {
		$em = $this->getDoctrine ()->getManager ();
		
		$new_information = null;
		$informations = $em->getRepository ( 'AcmeEventBundle:Information' )->findAll ();
		foreach ( $informations as $information ) {
			if (date ( 'Y-m-d', time () ) == $information->getDatumSend ()->format ( 'Y-m-d' ) && ! $information->getIsSend () && ! $information->getUserIsInformed ()) {
				$this->createMailOrganisator ( $information );
				$this->createMailUser ( $information );
				$information->setDatumSendExecute ( new \DateTime ( 'now' ) );
				$information->setActuellInformation ( 1 );
				$information->setIsSend ( 1 );
				$information->setUserIsInformed ( 1 );
				$new_information = $information;
			} else if (date ( 'Y-m-d', time () ) == $information->getDatum ()->add ( new \DateInterval ( 'P1D' ) )->format ( 'Y-m-d' ) && $information->getIsSend () && $information->getUserIsInformed ()) {
				$this->feedbackMailUser ( $information );
				// $information->setActuellInformation(2);
			}
			$em->persist ( $information );
			$em->flush ();
		}
		if (! is_null ( $new_information )) {
			$mail = $this->get ( 'common_mail' );
			$mail->checkActuellInformation ( $new_information );
		}
		
		return array ();
	}
	private function createMailOrganisator($information) {
		$em = $this->getDoctrine ()->getManager ();
		$organisator = $em->getRepository ( 'AcmeEventBundle:Information' )->findByPerson ( $information->getId () );
		// send mail zu organisator
		$email_to = $organisator ['email'];
		
		$subject = 'Organisator des SELV Event vom ' . $organisator ['datum']->format ( 'd.m.Y' );
		$message = $this->renderView ( 'AcmeCommonBundle:Cron:emailOrganisator.html.twig', array (
				'firstname' => $organisator ['firstname'],
				'eventmonat' => $organisator ['datum']->format ( 'F' ),
				'eventdatum' => $organisator ['datum']->format ( 'd.m.Y' ),
				'uid' => $organisator ['uid'],
				'email' => $organisator ['email'],
				'id' => $organisator ['id'] 
		) );
		$mail = $this->get ( 'common_mail' );
		$mail->sendMail ( $subject, $message, $email_to );
	}
	private function createMailUser($information) {
		$em = $this->getDoctrine ()->getManager ();
		$information = $em->getRepository ( 'AcmeEventBundle:Information' )->findByPerson ( $information->getId () );
		// Tabelle erstellen Mails senden
		$users = $em->getRepository ( 'AcmeUserBundle:User' )->findAllPerson ();
		
		$mail = $this->get ( 'common_mail' );
		
		foreach ( $users as $user ) {
			
			$inscription = $em->getRepository ( 'AcmeEventBundle:Inscription' )->findOneBy ( array (
					'information_id' => $information ['id'],
					'user_id' => $user ['id'] 
			) );
			
			if (empty ( $inscription )) {
				$inscription = new Inscription ();
				$email_to = $user ['email'];
				$inscription->setInformation ( $em->getRepository ( 'AcmeEventBundle:Information' )->find ( $information ['id'] ) );
				$inscription->setUser ( $em->getRepository ( 'AcmeUserBundle:User' )->find ( $user ['id'] ) );
				$inscription->setParticipate ( - 1 );
				$inscription->setDatum ( new \DateTime ( '0000-00-00' ) );
				$em->persist ( $inscription );
				$em->flush ();
			}
			
			$message = $this->renderView ( 'AcmeCommonBundle:Cron:emailUser.html.twig', array (
					'firstname' => $user ['firstname'],
					'eventmonat' => $information ['datum']->format ( 'F' ),
					'eventdatum' => $information ['datum']->format ( 'd.m.Y' ),
					'eventorganisator' => $information ['firstname'],
					'id' => $information ['id'] 
			) );
			$subject = 'Anmeldung SELV Event vom ' . $information ['datum']->format ( 'd.m.Y' );
			$mail->sendMail ( $subject, $message, $email_to );
		}
	}
	private function feedbackMailUser($information) {
		$em = $this->getDoctrine ()->getManager ();
		$information = $em->getRepository ( 'AcmeEventBundle:Information' )->findByPerson ( $information->getId () );
		// Tabelle erstellen Mails senden
		$users = $em->getRepository ( 'AcmeUserBundle:User' )->findAllPerson ();
		$mail = $this->get ( 'common_mail' );
		
		foreach ( $users as $user ) {
			$email_bcc = $user ['email'];
			$message = $this->renderView ( 'AcmeCommonBundle:Cron:emailFeedbackUser.html.twig', array (
					'firstname' => $user ['firstname'],
					'eventmonat' => $information ['datum']->format ( 'F' ),
					'eventdatum' => $information ['datum']->format ( 'd.m.Y' ),
					'eventorganisator' => $information ['firstname'],
					'id' => $information ['id'] 
			) );
			
			$subject = 'Feedback zum SELV Event vom ' . $information ['datum']->format ( 'd.m.Y' );
			$mail->sendMail ( $subject, $message, $email_to );
		}
	}
}
